package algorithm.sort;

import java.util.Arrays;

/**
 * @author LiShaowen
 * @date 2020/9/16 20:57
 */
public class BinarySearch {
    public static int binarySearch(Integer[] srcArray, int des) {
        //定义初始最小、最大索引
        int start = 0;
        int end = srcArray.length - 1;
        //确保不会出现重复查找，越界
        while (start <= end) {
            //计算出中间索引值
            int middle = (end + start)>>>1 ;//防止溢出
            if (des == srcArray[middle]) {
                return middle;
                //判断下限
            } else if (des < srcArray[middle]) {
                end = middle - 1;
                //判断上限
            } else {
                start = middle + 1;
            }
        }
        //若没有，则返回-1
        return -1;
    }

    public static void main(String[] args) {
        Integer[] srcArray = {1,2,4,5,6};
        System.out.println(BinarySearch.binarySearch(srcArray, 6));
    }
}
